package com.adderis.soft.domain.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.adderis.soft.domain.dao.ICasDao;
import com.adderis.soft.domain.model.Cas;

@Repository("casDao")
public class CasDaoImpl implements ICasDao{
	private static final Log log = LogFactory
			.getLog(CasDaoImpl.class);
	
	Calendar calendar = Calendar.getInstance();
	java.util.Date now = calendar.getTime();
	Timestamp nowtimesstamp  = new java.sql.Timestamp(now.getTime()); ;

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * {@inheritDoc}
	 */
	public void persist(Cas transientCas) {
		log.debug("Persisting NomCommerial instance");
		try {
			transientCas.setCreatetimestamp(nowtimesstamp);
			transientCas.setStatutcode("A");
			entityManager.persist(transientCas);
			
			log.debug("persist successful");

		} catch (RuntimeException re) {
			log.error("persist failed", re);
		}

	}

	
	public void remove(Cas persistentCas) {
		log.debug("removing Cas instance");
		try {
			persistentCas.setStatutcode("D");
			persistentCas.setDeletiontimesstamp(nowtimesstamp);
			entityManager.merge(persistentCas);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.debug("remove failed", re);
		}

	}

	
	public void remove(Integer CasId) {
		this.remove(this.findById(CasId));

	}

	
	public Cas merge(Cas detachedCas) {
		log.debug("merging Cas instance");
		try {
			detachedCas.setUpdatetimesstamp(nowtimesstamp);
			Cas result = entityManager.merge(detachedCas);
			entityManager.merge(detachedCas);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			return null;
		}
	}

	
	public Cas findById(Integer id) {
		log.debug("getting Cas instance with id: " + id);
		try {
			Cas instance = entityManager
					.find(Cas.class, id);
			log.debug("findById successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("findById failed", re);
			return null;
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	
	@SuppressWarnings("unchecked")
	public List<Cas> findAll() {
		log.debug("getting all Cas instance");
		try {
			Query query = entityManager.createQuery("SELECT c FROM Cas c");
			List<Cas> casList = (List<Cas>) query.getResultList();
			log.debug("findAll successful");
			return casList;

		} catch (RuntimeException re) {
			log.error("findAll failed", re);
			return new ArrayList<Cas>();
		}
		
	}

	
	@SuppressWarnings("unchecked")
	public List<Cas> search(String searchString) {
		log.debug("Search Cas instances with search string: " + searchString);
		try {
			Query query = entityManager.createQuery("select nc from Cas nc where nc.Cas like :searchString");
			query.setParameter("searchString", searchString);
			List<Cas> casList = (List<Cas>) query.getResultList();
			log.debug("search successful");
			return casList;
		} catch (RuntimeException re) {
			log.error("search failed", re);
			return new ArrayList<Cas>();
		}
	}

}

